package com.usian.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive>implements AdSensitiveService {

    @Resource
    private AdSensitiveMapper adSensitiveMapper;

    @Override
    public ResponseResult findByNameAndPage(SensitiveDto dto) {
        //1、判断dto是否为空
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、检查分页参数
        dto.checkParam();
        //3、分页条件编写并实现
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(dto.getName()!=null, AdSensitive::getSensitives,dto.getName());
        Page page = new Page(dto.getPage(),dto.getSize());
        IPage iPage = adSensitiveMapper.selectPage(page, queryWrapper);
        //4、数据封装
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int)iPage.getTotal());
        responseResult.setData(iPage.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //1、判断id是否存在
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、判断是否存在和状态是否使用
        AdSensitive byId = getById(id);
        if (byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3、执行删除
        removeById(id);
        //4、返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult saveAdSensitive(AdSensitive adSensitive) {
        //1、判断参数是否存在
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //设置创建时间的参数
        adSensitive.setCreatedTime(new Date());
        //保存
        save(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        //1、判断参数是否存在
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改
        updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public List<AdSensitive> findAll() {
        return adSensitiveMapper.selectList(null);
    }
}
